import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import com.RspInterface 1.0
import com.AlmListModel 1.0
import com.ComboData 1.0

import com.QStringListModel 1.0


//强平单管理（查询、撤单）
Item {
    id: forceQuery
    property var tabName
    property string listName: "ForceOrder"
    property AlmListModel checkListModel
    //客户信息
    property RspInterface custDetail2
    property ComboData branchModel: g_MainQuo.getMyBranchs()
    //客户级别下拉
    property ComboData custGradeModel

    property var sectionSpace: 10
    property var kvSpace: 2
    property var col1width: 202

    //    signal freshData
    ColumnLayout {
        anchors.fill: parent
        RowLayout {
            anchors.top: parent.top
            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            //            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter

            Text {
                text: "代理商" + colon
                anchors.left: parent.left
            }
            ComboBox {
                id: cmbBranch
                model: branchModel
                Layout.preferredWidth: 140
                onCurrentIndexChanged: {
                    custGradeModel = null
                    custGradeModel = g_MainQuo.getCustGradeByBranchs(
                                branchModel.get(currentIndex))
                    for (var i = 0; i < custGradeModel.rowCount(); ++i) {
                        console.log("custGradeModel " + custGradeModel.get(i))
                    }
                }
            }

            Text {
                id: codeT
                text: "合约代码" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: parent.left
                anchors.leftMargin: col1width
            }

            ComboBox {
                id: cmbProd
                Layout.preferredWidth: 90
                Layout.alignment: Qt.AlignHCenter
                anchors.left: codeT.right
                anchors.leftMargin: kvSpace
                model: prodModel //["客户", "客户级别", "代理机构", "账户类型"]
                Component.onCompleted: {
                    //                    var type = ["01", "02"]
                    var type = ["02"]
                    var modelProd = g_MainQuo.getSomeProdCode(type)
                    prodModel.append({
                                         text: " "
                                     })
                    for (var i = 0; i < modelProd.length; ++i) {
                        prodModel.append({
                                             text: modelProd[i]
                                         })
                    }

                    //                    model = ["01", "02"]
                    //                    console.log("QString type in qml " + g_MainQuo.getTypeTest(
                    //                                    ))
                }
            }

            CheckBox {
                id: check1
                text: "已经报入"
                anchors.left: parent.left
                anchors.leftMargin: 385
            }
            CheckBox {
                id: check2
                text: "部分成交"
                anchors.left: check1.right
                anchors.leftMargin: sectionSpace
            }
            CheckBox {
                id: check3
                text: "全部成交"
                anchors.left: check2.right
                anchors.leftMargin: sectionSpace
            }
            CheckBox {
                id: check4
                text: "已经撤销"
                anchors.left: check3.right
                anchors.leftMargin: sectionSpace
            }
            CheckBox {
                id: check5
                text: "失败委托"
                anchors.left: check4.right
                anchors.leftMargin: sectionSpace
            }
        }
        RowLayout {
            //            anchors.top: parent.top
            //            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            //            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter
            Text {
                text: "客户号" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: parent.left
            }
            TextField {
                id: acct_No
                Layout.alignment: Qt.AlignHCenter
                Layout.preferredWidth: 140
            }
            Text {
                id: tellerT
                text: "操作员" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: parent.left

                anchors.leftMargin: col1width
            }
            TextField {
                Layout.preferredWidth: 85
                anchors.left: tellerT.right
                anchors.leftMargin: kvSpace
                id: teller_id
                Layout.alignment: Qt.AlignHCenter
            }

            Text {
                id: acctT
                text: "账户类型" + colon
                Layout.alignment: Qt.AlignHCenter

                anchors.left: parent.left
                anchors.leftMargin: 385
            }
            ComboBox {
                id: cmbAcctType
                Layout.alignment: Qt.AlignHCenter
                Layout.preferredWidth: 70
                anchors.left: acctT.right
                anchors.leftMargin: kvSpace
                model: acctTypeModel
            }
            Text {
                id: custGradeT
                text: "客户级别" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: cmbAcctType.right
                anchors.leftMargin: sectionSpace
            }
            ComboBox {
                id: cmbCustGrade
                Layout.alignment: Qt.AlignHCenter
                anchors.left: custGradeT.right
                anchors.leftMargin: kvSpace
                model: custGradeModel
                onCurrentIndexChanged: {
                    console.log("gradeName " + currentText)
                }
            }
        }
        RowLayout {
            //            anchors.top: parent.top
            //            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            //            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter
            z: 20 //弹出的日历不会被盖
            Text {
                id: beginT
                text: "开始日期" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.leftMargin: 2
            }
            DatePicker {
                id: b_date
                Layout.preferredWidth: 110
                anchors.left: beginT.right
                anchors.leftMargin: kvSpace
            }

            Text {
                id: endT
                text: "结束日期" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: parent.left
                anchors.leftMargin: col1width
            }
            DatePicker {
                id: e_date
                Layout.preferredWidth: 110
                anchors.left: endT.right
                anchors.leftMargin: kvSpace
            }
            //            Item {
            //                Layout.fillWidth: true
            //            }
            Button {
                text: "查询"
                anchors.right: parent.right
                anchors.rightMargin: 20
                onClicked: {
                    var bDate = b_date.getDateStr()
                    var eDate = e_date.getDateStr()
                    if (parseInt(bDate) > parseInt(eDate)) {
                        tipInput.text = "开始日期不能大于结束日期!"
                        tipInput.open()
                        return
                    }
                    queryOrder()
                }
            }
            //            Item {
            //                Layout.preferredWidth: 20
            //            }
        }
        CheckListFrame {
            id: forceCustTab
            Layout.fillHeight: true
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 10
            tablelistName: listName

            onRowDoubleChange: {
                console.log("onRowDoubleChange:" + row)
                var acctNo = checkListModel.getCellText(row, 0)
                custDetail2 = g_MainQuo.getCustDetail(acctNo)
            }
            onRightClick: {
                rBtnMenu.row = row
                rBtnMenu.col = col - 1
                rBtnMenu.model = checkListModel
                rBtnMenu.popup()
            }
            onCheckRowChange: {

                if (row >= 0) {
                    //单个勾选的处理
                    var state = checkListModel.getCellText(row, 9)
                    console.log("onCheckRowChange:" + row + " bcheck:" + bCheck + " state:" + state)
                    if (state === "1" || state === "2") {
                        //已经报入,部分成交
                        checkListModel.setCheck(row, bCheck)
                        checkListModel.resetModel()
                    }
                } else {
                    for (var i = 0; i < checkListModel.rowCount(); ++i) {
                        var orderstate = checkListModel.getCellText(i, 9)
                        if (orderstate === "1" || orderstate === "2")
                            //已经报入,部分成交
                            checkListModel.setCheck(row, bCheck)
                    }
                    checkListModel.resetModel()
                }
            }
        }

        CustDetail {
            custDetail: custDetail2
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.right: parent.right
            //            anchors.rightMargin: 10
            anchors.rightMargin: 180
            anchors.bottom: parent.bottom
            Layout.preferredHeight: 150
            thirdColumnTopMargin: 26
        }

        //        }
    }

    CheckBox {
        text: "全选可撤单"
        anchors.right: btnCancel.left
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 132
        onCheckedChanged: {
            forceCustTab.checkRowChange(-1, checked)
        }
    }

    Button {
        id: btnCancel
        text: "撤销"
        anchors.right: btnSetColumn.left
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 130
        onClicked: {
            console.log("cance order")
            for (var i = 0; i < checkListModel.rowCount(); ++i) {
                var orderNo = checkListModel.getCellText(i, 10)
                if (checkListModel.getCheck(i) && orderNo.length > 0) {
                    var msg = g_MainQuo.cancelForceOrder(orderNo)
                    if (msg.length > 0) {
                        tipInput.text = msg
                        tipInput.open()
                    }
                }
            }
        }
    }
    Button {
        id: btnSetColumn
        text: "设置列"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 130
        onClicked: {
            colSet.visible = true
            //            colSet.refList = forceCustTab
            colSet.loadColumnCfg(listName)
        }
    }
    Connections {
        target: colSet
        onUpdateUIColumn: {
            if (sList == listName) {
                forceCustTab.updateColumn(listName)
                queryOrder()
            }
        }
    }
    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }
    ListModel {
        id: prodModel //合约下拉
    }

    //    ListModel {
    //        id: gradeModel //客户级别下拉
    //    }
    function queryOrder() {
        var req = "#begin_date=" + b_date.getDateStr()
        req += "#end_date=" + e_date.getDateStr()
        req += "#branch_id=" + branchModel.get(cmbBranch.currentIndex)
        if (acct_No.text.length > 0)
            req += "#acct_no=" + acct_No.text

        if (cmbProd.currentIndex != 0)
            req += "#instid=" + cmbProd.currentText

        if (teller_id.text.length > 0)
            req += "#f_teller_id=" + teller_id.text

        if (cmbAcctType.currentIndex != 0)
            req += "#acct_type=" + cmbAcctType.model.get(
                        cmbAcctType.currentIndex).value

        if (cmbCustGrade.currentIndex != 0)
            req += "#cust_type=" + custGradeModel.get(cmbCustGrade.currentIndex)

        var stateHead = "#order_status_set="
        var status = stateHead
        //fc_order_state｜1｜已经报入｜∧fc_order_state｜2｜部分成交｜∧fc_order_state｜3｜全部成交｜∧fc_order_state｜4｜已经撤销｜∧fc_order_state｜5｜失败委托｜∧
        if (check1.checked)
            status += "1｜"
        if (check2.checked)
            status += "2｜"
        if (check3.checked)
            status += "3｜"
        if (check4.checked)
            status += "4｜"
        if (check5.checked)
            status += "5｜"
        if (status.length > stateHead.length) {
            status += "∧"
            req += status
        }

        var rsp = g_MainQuo.queryForceOrder(req)
        checkListModel = rsp.getAlmModel("alm_result", listName, true)
        //        checkListModel.setShowCheck(true, false)
        console.log("req:" + req)
    }
}
